//
// Created by antler on 2018/11/12.
//

#ifndef LUA_GLOBALMACROS_H
#define LUA_GLOBALMACROS_H

#define VERSION_MAJOR    "2"
#define VERSION_MINOR    "0"
#define VERSION_RELEASE  "1"

#define VERSION_CODE      1
#define VERSION_NAME      VERSION_MAJOR "." VERSION_MINOR "." VERSION_RELEASE

#define XSP_FILE_EXT ".xsp"
#define ZIP_FILE_EXT ".zip"
#define XSC_FILE_EXT ".xsc"
#define NOT_BYTECODE_FILE_EXT ".lua"
#define BYTECODE_FILE_EXT ".luac"

#define NOT_BYTECODE_FILE_EXT_MARK "?.lua"
#define BYTECODE_FILE_EXT_MARK "?.luac"

#define MANIFEST_JSON "manifest.json"
#define MANIFEST_JSON_KEY_MAIN "main"
#define MANIFEST_JSON_KEY_BIN_SHA1 "bin_sha1"
#define MANIFEST_JSON_KEY_TENGINE "tengine"
#define MANIFEST_JSON_KEY_VERSION "version"

#define SAFE_DELETE(p) { if ((p)) { delete ((p)); ((p)) = nullptr; } }
#define SAFE_FREE(p) { if ((p)) { free((p)); ((p)) = nullptr; } }

#define RGB565_R(p) ((((p) & 0xF800) >> 11) << 3)
#define RGB565_G(p) ((((p) & 0x7E0 ) >> 5)  << 2)
#define RGB565_B(p) ( ((p) & 0x1F  )        << 3)
#define MAKE_RGB565(r, g, b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))

#define SetARGB(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
#define SetRGB(r, g, b) (((r) << 16) | ((g) << 8) | (b))

#define ARGB_A(color)      (((color) >> 24) & 0xFF)
#define ARGB_R(color)      (((color) >> 16) & 0xFF)
#define ARGB_G(color)      (((color) >>  8) & 0xFF)
#define ARGB_B(color)      (((color) >>  0) & 0xFF)

#ifndef SK_A32_SHIFT
#  ifdef SK_CPU_BENDIAN
#    define SK_R32_SHIFT    24
#    define SK_G32_SHIFT    16
#    define SK_B32_SHIFT    8
#    define SK_A32_SHIFT    0
#  else
#    define SK_R32_SHIFT    0
#    define SK_G32_SHIFT    8
#    define SK_B32_SHIFT    16
#    define SK_A32_SHIFT    24
#  endif
#endif

#define ARGB_PackedA32(packed)      ((uint32_t)((packed) << (24 - SK_A32_SHIFT)) >> 24)
#define ARGB_PackedR32(packed)      ((uint32_t)((packed) << (24 - SK_R32_SHIFT)) >> 24)
#define ARGB_PackedG32(packed)      ((uint32_t)((packed) << (24 - SK_G32_SHIFT)) >> 24)
#define ARGB_PackedB32(packed)      ((uint32_t)((packed) << (24 - SK_B32_SHIFT)) >> 24)

#endif //LUA_GLOBALMACROS_H
